<template>
  <div>
    <article class="main-breadcrumb">
      <el-breadcrumb separator=">" class="main-breadcrumb-wrap">
        <el-breadcrumb-item>
          <span class="main-breadcrumb-home">巡检管理</span>
        </el-breadcrumb-item>
        <el-breadcrumb-item>
          <span class="main-breadcrumb-home">巡检任务</span>
        </el-breadcrumb-item>
      </el-breadcrumb>
    </article>
    <div class="nx-content">
      <div class="flex-bet">
        <el-form ref="searchForm" :inline="true" :model="searchForm" class="demo-form-inline">
          <el-form-item label="">
            <el-cascader v-if="userType == 0" v-model="searchForm.companyId" placeholder="请选择公司名称" :options="companyArray" :props="{
                checkStrictly: true,
                value: 'id',
                label: 'name',
                children: 'childList',
              }" style="width: 11rem !important" @change="changeTreeItem" clearable></el-cascader>
          </el-form-item>
          <el-form-item label="">
            <el-select v-model="searchForm.baseId" placeholder="请选择基地名称" @change="baseChange" style="width: 11rem !important" clearable>
              <el-option v-for="item in baseArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="">
            <el-select v-model="searchForm.boxId" placeholder="请选择控制箱名称" @change="boxChange" style="width:11rem!important"
              clearable>
              <el-option v-for="item in boxArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item> -->
          <el-form-item label="">
            <el-select v-model="searchForm.fishpondId" placeholder="请选择鱼池名称" @change="onSubmit" style="width: 11rem !important" clearable>
              <el-option v-for="item in fishpondArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="">
            <el-input maxlength="50" placeholder="请输入设备编码" v-model="searchForm.deviceCode" style="width:11rem!important"
              clearable></el-input>
          </el-form-item> -->
          <!-- <el-form-item label="">
            <el-select v-model="searchForm.deviceStatus" placeholder="请选择设备状态" @change="onSubmit"
              style="width:11rem!important" clearable>
              <el-option label="离线" value="0"></el-option>
              <el-option label="在线" value="1"></el-option>
              <el-option label="告警" value="2"></el-option>
            </el-select>
          </el-form-item> -->
          <!-- <el-form-item label="">
            <el-select v-model="searchForm.deviceType" placeholder="请选择数值设备类型" @change="onSubmit"
              style="width:11rem!important" clearable>
              <el-option label="电流" value="0"></el-option>
              <el-option label="电压" value="1"></el-option>
              <el-option label="水位" value="2"></el-option>
              <el-option label="气压" value="3"></el-option>
              <el-option label="流量" value="4"></el-option>
              <el-option label="溶氧" value="5"></el-option>
              <el-option label="相序状态" value="6"></el-option>
            </el-select>
          </el-form-item> -->
          <el-form-item>
            <div v-allow="'030201'" class="search-btn" @click="onSubmit"></div>
          </el-form-item>
          <el-form-item>
            <div v-allow="'030201'" class="reset-btn" @click="resetForm"></div>
          </el-form-item>
        </el-form>
        <div class="left-title flex">
          <!-- <div v-allow="'030203'" class="add-btn" style="margin-top: -1rem !important" @click="handleAdd()">
            添加巡检任务
          </div> -->
        </div>
      </div>
      <NxTable :table-cols="tableCols" :table-data="tableData" :pag-ination="pagination" class="table-wrap" @refresh="refresh">
        <template slot="images" slot-scope="scope">
          <el-image style="height: 50px" :src="scope.row.images != null ? scope.row.images.split(',')[0] : ''
            " :preview-src-list="scope.row.images != null ? scope.row.images.split(',') : []
    " fit="contain">
          </el-image>
        </template>
        <template slot="status" slot-scope="scope">
          <span v-if="scope.row.status == 0">未处理</span>
          <span v-if="scope.row.status == 1">已处理</span>
        </template>

        <template slot="operation" slot-scope="scope">
          <!-- <span v-allow="'030202'" @click="handleHistory(scope.row)" class="nx-btn nx-edit">详情</span> -->
          <span v-allow="'030204'" @click="handleEdit(scope.row)" class="nx-btn nx-edit">编辑</span>
          <span v-allow="'030205'" @click="handleDelete(scope.row)" class="nx-btn nx-del">删除</span>
        </template>
      </NxTable>
    </div>
    <AddEditDialog :addEditId="addEditId" :addEditStatus="addEditStatus" :addEditIsDialog="addEditIsDialog">
    </AddEditDialog>
  </div>
</template>
<script>
import NxTable from '@/components/NxTable.vue'
import AddEditDialog from './components/addEditDialog.vue'
import { Page } from '@/utils/pagination'
import { getBoxFishponds } from '@/api/inspect'
import { getCompanyInfoTree } from '@/api/company'
import { getSysCompanyBases, getObjBoxs, getDataTaskInspectList, delDataTaskInspect } from '@/api/system'
import mixin from '@/api/CompanyInfoTree.js'
export default {
  mixins: [mixin],
  components: {
    NxTable,
    AddEditDialog
  },
  // 表格数据
  data() {
    return {
      // 查询字段
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        companyId: '',
        baseId: '',
        boxId: '',
        fishpondId: '',
        deviceCode: '',
        deviceStatus: '',
        deviceType: ''
      },
      // 表格字段
      tableCols: [
        {
          label: '公司名称',
          prop: 'companyName',
          align: 'center',
          width: 250
        },
        {
          label: '基地名称',
          prop: 'baseName',
          align: 'center',
          width: 150
        },
        {
          label: '发送内容',
          prop: 'content',
          align: 'center'
        },
        {
          label: '任务处理人名称',
          prop: 'dealUserName',
          align: 'center'
        },
        {
          label: '鱼池名称',
          prop: 'fishpondName',
          align: 'center'
        },
        {
          label: '发送图片',
          slot: 'images',
          align: 'center'
        },
        {
          label: '发送时间',
          prop: 'sendDate',
          align: 'center'
        },

        {
          label: '处理状态',
          slot: 'status',
          align: 'center'
        }

        // {
        //   label: '操作',
        //   slot: 'operation',
        //   align: 'center',
        //   width: 180,
        //   fixed: 'right'
        // }
      ],
      tableData: [],
      pagination: new Page(),
      crumbsStatus: '', // 三级面包屑名称
      typeDialog: false,
      addEditIsDialog: false, // 添加编辑页面状态
      addEditStatus: 'add',
      addEditId: '', // 添加编辑rowid
      companyArray: [], // 公司集合
      baseArray: [], // 基地集合
      boxArray: [], // 控制箱集合
      fishpondArray: [], // 鱼池集合
      userType: '' // 用户类型（0超级管理员，1公司管理员，2公司员工）
    }
  },
  // 监听当baseId发生变化时候自动调用命令
  watch: {
    'searchForm.baseId': {
      handler(newValue, oldValue) {
        this.getObjBoxs()
        this.getBoxFishponds()
      }
    }
  },
  // 初始化加载
  mounted() {
    this.userType = Number(window.localStorage.getItem('userType'))
    this.searchForm.userType = this.userType
    this.getCompanyInfoTree()
    this.getList()
  },
  // 自定义方法
  methods: {
    handleHistory(row) {
      this.$router.push({
        path: `/device/sysSensorHistory?id=` + row.id
      })
    },
    // 查询
    onSubmit() {
      this.pagination.pageNum = 1
      this.getList()
    },
    // 分页查询
    refresh(val) {
      this.searchForm.pageNum = val.pageNum
      this.searchForm.pageSize = val.pageSize
      this.getList()
    },
    // 重置
    resetForm() {
      this.searchForm = {
        pageNum: 1,
        pageSize: 10,
        companyId: '',
        baseId: '',
        boxId: '',
        fishpondId: '',
        deviceCode: '',
        deviceStatus: '',
        deviceType: ''
      }

      this.onSubmit()
    },
    // 查询列表
    getList() {
      getDataTaskInspectList(this.searchForm).then((res) => {
        if (res.code == 200) {
          this.tableData = res.data.data
          this.pagination.count = res.data.total
        }
      })
    },
    // 删除
    handleDelete(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          delDataTaskInspect({ id: row.id }).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: res.msg
              })
              this.getList()
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 添加
    handleAdd() {
      this.addEditId = ''
      this.addEditStatus = 'add'
      this.addEditIsDialog = true
    },
    // 编辑
    handleEdit(row) {
      this.addEditId = row.id + ''
      this.addEditStatus = 'edit'
      this.addEditIsDialog = true
    },
    // 编辑添加取消关闭
    closeAddEdit() {
      this.addEditIsDialog = false
    },
    // 面包屑
    crumbsClick() {
      this.typeDialog = false
    },
    // 用户类型切换
    typeChange() {
      this.searchForm.companyId = ''
      this.companyArray = []

      this.searchForm.roleId = ''
      this.baseArray = []

      // 获取公司列表
      this.getCompanyInfoTree()

      // 查询
      this.onSubmit()
    },
    // 获取公司列表
    // getCompanyInfoTree() {
    //   if (this.searchForm.userType == 0) {
    //     // 超级管理员，公司只有一个总公司
    //     getCompanyInfoTree().then((res) => {
    //       if (res.code == 200) {
    //         this.companyArray = res.data
    //       }
    //     })
    //   } else {
    //     getCompanyInfoTree({ id: 0 }).then((res) => {
    //       if (res.code == 200) {
    //         this.companyArray = res.data
    //       }
    //     })
    //   }
    // },
    // 公司下拉切换
    changeTreeItem(val) {
      if (val) {
        this.searchForm.companyId = val[val.length - 1]
      } else {
        this.searchForm.companyId = ''
      }

      this.searchForm.baseId = ''
      this.baseArray = []

      this.searchForm.boxId = ''
      this.boxArray = []

      this.searchForm.fishpondId = ''
      this.fishpondArray = []

      this.getList()

      // 获取基地列表
      this.getSysCompanyBases()
    },
    // 获取基地列表
    // getSysCompanyBases() {
    //   if (
    //     this.searchForm.companyId === '' ||
    //     this.searchForm.companyId == null
    //   ) {
    //     return
    //   }

    //   getSysCompanyBases({ id: this.searchForm.companyId }).then((res) => {
    //     if (res.code == 200) {
    //       this.baseArray = res.data
    //     }
    //   })
    // },
    // 基地下拉切换
    baseChange() {
      this.searchForm.boxId = ''
      this.boxArray = []

      // 查询
      this.onSubmit()

      // 获取控制箱列表
      this.getObjBoxs()
      this.getBoxFishponds()
    },
    // 获取控制箱列表
    getObjBoxs() {
      if (this.searchForm.baseId === '' || this.searchForm.baseId == null) {
        return
      }

      getObjBoxs({ id: this.searchForm.baseId }).then((res) => {
        if (res.code == 200) {
          this.boxArray = res.data
        }
      })
    },
    // 控制箱下拉切换
    boxChange() {
      this.searchForm.fishpondId = ''
      this.fishpondArray = []

      // 查询
      this.onSubmit()

      // 获取鱼池列表
      this.getBoxFishponds()
    },
    // 获取鱼池列表
    getBoxFishponds() {
      // if (
      //   this.searchForm.boxId === "" ||
      //   this.searchForm.boxId == null
      // ) {
      //   return;
      // }

      getBoxFishponds(this.searchForm).then((res) => {
        if (res.code == 200) {
          this.fishpondArray = res.data
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.main-breadcrumb {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  height: 76px;
  border-bottom: solid 20px #f2f2f2;
  padding-left: 20px;

  .main-breadcrumb-wrap {
    font-size: 16px;
    line-height: 68px;

    .main-breadcrumb-home {
      cursor: pointer;
      color: #666666;
      font-weight: bold;
    }

    .main-breadcrumb-sp {
      color: #666666;
      font-weight: bold;
    }
  }
}
</style>
